package com.llyb.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RabbitMqConfiguration implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);
    }


    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            log.info("发送到交换机成功，数据： " + correlationData );
        }else {
            log.info("发送到交换机失败，数据：{},失败原因： {} ",correlationData,cause);
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {

        log.info("消息体：{}",returnedMessage.getMessage().getBody());
        log.info("应答码: {}",returnedMessage.getReplyCode());
        log.info("描述：{}",returnedMessage.getReplyText());
        log.info("交换机是：{}",returnedMessage.getExchange());
        log.info("路由是： {}",returnedMessage.getRoutingKey());
    }
}
